package com.lxmes.jiesuan.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxmes.business.domain.CcCaigouMain;
import com.lxmes.business.domain.CcRukuMain;
import com.lxmes.business.service.ICcRukuMainService;
import com.lxmes.system.service.ISysDictDataService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.jiesuan.domain.JiesuanZhikuan;
import com.lxmes.jiesuan.service.IJiesuanZhikuanService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 支款记账Controller
 * 
 * @author zhangzhiyu
 * @date 2022-09-26
 */
@Api(tags="结算中心-支款")
@RestController
@RequestMapping("/jiesuan/zhikuan")
public class JiesuanZhikuanController extends BaseController
{
    @Autowired
    private IJiesuanZhikuanService jiesuanZhikuanService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ICcRukuMainService rukuMainService;
    /**
     * 查询支款记账列表
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:zhikuan:list')")
    @GetMapping("/list")
    public TableDataInfo list(JiesuanZhikuan jiesuanZhikuan)
    {
        startPage();
        List<JiesuanZhikuan> list = jiesuanZhikuanService.selectJiesuanZhikuanList(jiesuanZhikuan);
        return getDataTable(list);
    }
    /**
     * 手机端采购单列表
     */
    @ApiOperation(value="结算中心支款列表")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ywType", value = "业务类型", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "ywId", value = "业务单号", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "cusId", value = "客户名称", paramType="query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "fkfs", value = "付款方式", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "queryZfDate1", value = "付款开始时间", paramType="query", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "queryZfDate2", value = "付款结束时间", paramType="query", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = CcCaigouMain.class)})
    @PreAuthorize("@ss.hasPermi('mobile:zhikuan:list')")
    @GetMapping("/mobileList")
    public TableDataInfo mobileList(
            @RequestParam(value ="ywType",required = false) String ywType,
            @RequestParam(value ="ywId",required = false) String ywId,
            @RequestParam(value ="cusId",required = false) Long cusId,
            @RequestParam(value ="fkfs",required = false) String fkfs,
            @RequestParam(value ="queryZfDate1",required = false) Date queryZfDate1,
            @RequestParam(value ="queryZfDate2",required = false) Date queryZfDate2
        )
    {
        startPage();
        JiesuanZhikuan jiesuanZhikuan=new JiesuanZhikuan();
        jiesuanZhikuan.setYwId(ywType);
        jiesuanZhikuan.setYwId(ywId);
        jiesuanZhikuan.setCusId(cusId);
        jiesuanZhikuan.setFkfs(fkfs);
        jiesuanZhikuan.setQueryZfDate1(queryZfDate1);
        jiesuanZhikuan.setQueryZfDate2(queryZfDate2);
        List<JiesuanZhikuan> list = jiesuanZhikuanService.selectJiesuanZhikuanList(jiesuanZhikuan);
        return getDataTable(list);
    }

    /**
     * 导出支款记账列表
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:zhikuan:export')")
    @Log(title = "支款记账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JiesuanZhikuan jiesuanZhikuan)
    {
        List<JiesuanZhikuan> list = jiesuanZhikuanService.selectJiesuanZhikuanList(jiesuanZhikuan);
        ExcelUtil<JiesuanZhikuan> util = new ExcelUtil<JiesuanZhikuan>(JiesuanZhikuan.class);
        util.exportExcel(response, list, "支款记账数据");
    }

    /**
     * 获取支款记账详细信息
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:zhikuan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jiesuanZhikuanService.selectJiesuanZhikuanById(id));
    }

    @GetMapping(value = "/getSurplusZhiKuan/{ywId}")
    public AjaxResult getSurplusZhiKuan(@PathVariable("ywId") String ywId)
    {
        return AjaxResult.success(jiesuanZhikuanService.selectJsSurplusZhiKuan(ywId));
    }
    /**
     * 手机端获取采购详细信息
     */
    @ApiOperation(value="支款明细细信息")
    @ApiImplicitParam(name = "id", value = "支款id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/getZhiKuan/{id}")
    public AjaxResult mobileGetInfo(@PathVariable("id") Long  id)
    {
        return AjaxResult.success(jiesuanZhikuanService.selectJiesuanZhikuanById(id));
    }

    /**
     * 新增支款记账
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:zhikuan:add')")
    @Log(title = "支款记账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JiesuanZhikuan jiesuanZhikuan)
    {
        return toAjax(jiesuanZhikuanService.insertJiesuanZhikuan(jiesuanZhikuan));
    }

    /**
     * 修改支款记账
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:zhikuan:edit')")
    @Log(title = "支款记账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JiesuanZhikuan jiesuanZhikuan)
    {
        return toAjax(jiesuanZhikuanService.updateJiesuanZhikuan(jiesuanZhikuan));
    }
    /**
     * 新增支款记账
     */
    @ApiOperation(value="新增支款记账")
    @PreAuthorize("@ss.hasPermi('mobile:zhikuan:add')")
    @Log(title = "新增支款记账", businessType = BusinessType.INSERT)
    @PostMapping("/mobileAdd")
    public AjaxResult mobileAdd(@RequestBody JiesuanZhikuan jiesuanZhikuan)
    {
        return toAjax(jiesuanZhikuanService.insertJiesuanZhikuan(jiesuanZhikuan));
    }
    @ApiOperation(value="获取支款业务类型")
    @GetMapping("/getZkYwType")
    public AjaxResult getZkYwType(){
        return AjaxResult.success( dictDataService.selectDictDataByType("zk_yw_type"));
    }
    @ApiOperation(value="获取支款业务单号",tags = "入库方式 1-原材料 2 产成品")
    @ApiImplicitParam(name = "rukuFlag", value = "入库方式", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping("/getZkYwDan/{rukuFlag}")
    public AjaxResult getZkYwDan(@PathVariable("rukuFlag") String  rukuFlag){
        List<CcRukuMain> list = rukuMainService.selectCcRukuMainByRukuFlag(rukuFlag);
        JSONArray jsonArray=new JSONArray();
        for (CcRukuMain main:list) {
            JSONObject object=new JSONObject();
            object.put("value",main.getDanhao());
            object.put("label",main.getLot());
            //object.put("cusName",main.getSupplyName());
            //object.put("cusId",main.getSupplyId());
            object.put("money",main.getTotalPrice());
            jsonArray.add(object);
        }
        return AjaxResult.success(jsonArray);
    }
    @ApiOperation(value="获取支款方式")
    @GetMapping("/getZkFs")
    public AjaxResult getZkFs(){
        return AjaxResult.success( dictDataService.selectDictDataByType("sz_fs"));
    }

    /**
     * 删除支款记账
     */
    @PreAuthorize("@ss.hasPermi('jiesuan:zhikuan:remove')")
    @Log(title = "支款记账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jiesuanZhikuanService.deleteJiesuanZhikuanByIds(ids));
    }
}
